<?php
/**
 * AOP切面编程之验证
 * Created by PhpStorm.
 * User: Egor <http://gitee.com/seosite>
 * Date: 2018/4/11
 * Time: 下午11:09
 */

namespace Wap\Controller;

use Think\Controller;

class CheckFactoryController extends Controller
{

    /**
     * @desc 检查是否登录
     * @author Egor<http://gitee.com/seosite>
     * @return checkFamily
     */
    public static function isLogin()
    {
        $checkFamily = new checkFamily();
        $checkFamily->addCheck(new is_login());
        return $checkFamily;
    }


    /**
     * @desc 检查是否设置param
     * @author Egor<http://gitee.com/seosite>
     * @return checkFamily
     */
    public static function isParamSet()
    {
        $checkFamily = new checkFamily();
        $checkFamily->addCheck(new isParamSet());
        return $checkFamily;
    }

    /**
     * @desc 检查param下是否有指定的子参数
     * @author Egor<http://gitee.com/seosite>
     * @return checkFamily
     */
    public static function isChildParamSet()
    {
        $checkFamily = new checkFamily();
        $checkFamily->addCheck(new isChildParamSet());
        return $checkFamily;
    }

    public static function inletSet()
    {
        $checkFamily = new checkFamily();
        $checkFamily->addCheck(new isInletParamSet());
        return $checkFamily;
    }

    public static function checkDetailAuth()
    {
        $checkFamily = new checkFamily();
        $checkFamily->addCheck(new checkDetailAuth());
        return $checkFamily;
    }


}

/**
 * Interface Extension
 * @package App\Controller
 */
interface Extension
{
    public function before($param);

    public function end($param);
}

/**
 * check admin user
 * Class is_admin
 * @package App\Controller
 */
class is_admin implements Extension
{
    public function before($param)
    {
    }

    public function end($param)
    {
    }
}


/**
 * check user login
 * Class is_login
 * @package App\Controller
 */
class is_login implements Extension
{
    private $userInfo;

    public function __construct()
    {
        $userInfo = decode_token();
        $this->userInfo = empty($userInfo) ? "" : (object)$userInfo;
    }

    public function before($param)
    {
        $api = new ApiController();
        if (!array_key_exists('token',get_post_vars())) return $api->output(showErrorMsg('token is not set!',100002),I('post.'));
        if (empty($this->userInfo)) return $api->output(showErrorMsg('user token check failure!',100004), I('post.'));
        return true;
    }

    public function end($param)
    {
    }
}


/**
 * check param
 * Class isParamSet
 * @package App\Controller
 */
class isParamSet implements Extension{

    public function before($param)
    {
        $api = new ApiController();
        if (!array_key_exists('param',get_post_vars())) return $api->output(showErrorMsg('param is not set!',100002),I('post.'));
        return true;
    }

    public function end($param)
    {
    }
}


/**
 * check param child
 * Class isChildParamSet
 * @package App\Controller
 */
class isChildParamSet implements Extension{

    public function before($param)
    {
        $api = new ApiController();

        if (empty($param)) return $api->output(showErrorMsg('param child is null!',100002),I('post.'));

        $paramArr = explode(',',$param);
        foreach ($paramArr as &$value){
            if (!array_key_exists($value,get_param_vars())) return $api->output(showErrorMsg($value.' is not set!',100002),I('post.'));
        }
        unset($value);

        return true;
    }

    public function end($param)
    {
    }
}

class isInletParamSet implements Extension{

    public function before($param)
    {
        $api = new ApiController();

        if (empty($param)) return $api->output(showErrorMsg('param is null!',100002),I('post.'));

        $paramArr = explode(',',$param);
        foreach ($paramArr as &$value){
            if (!array_key_exists($value,get_post_vars())) return $api->output(showErrorMsg($value.' is not set!',100002),I('post.'));
        }
        unset($value);

        return true;
    }

    public function end($param)
    {
    }
}

/**
 *
 * 这里是针对所有的详情
 *
 * Class checkAuth
 * @package Wap\Controller
 */
class checkDetailAuth implements Extension{

    public function before($param)
    {
        $token = decode_token($_POST['token']);

        $is_admin = !empty($token) && $token['is_admin']==1 ?1:0;

        $is_login = !empty($token)?1:0;

        if (!$is_admin){
            $api = new ApiController();

            $paramArr = explode(',',$param);

            $work_id = $paramArr[0];
            $type = $paramArr[1];
            $pwd = $paramArr[2];

            $info = M($type)->find($work_id);

            if (empty($info)) return $api->output(showErrorMsg('数据不存在',100002),I('post.'));
            if ($info['is_show']!=1) return $api->output(showErrorMsg('数据已下架',100002),I('post.'));

            switch ($type){
                case 'video':
                    if ($info['is_draft']!=2) return $api->output(showErrorMsg('数据未发布',100002),I('post.'));
                    $order_type = 1;
                    break;
                case 'direct':
                    if ($info['is_draft']!=2) return $api->output(showErrorMsg('数据未发布',100002),I('post.'));
                    $order_type = 2;
                    break;
                case 'mp3':
                    if ($info['is_draft']!=2) return $api->output(showErrorMsg('数据未发布',100002),I('post.'));
                    $order_type = 3;
                    break;
                case 'audio':
                    if ($info['is_draft']!=2) return $api->output(showErrorMsg('数据未发布',100002),I('post.'));
                    $order_type = 14;
                    break;
                case 'report':
                    $order_type = 4;
                    break;
                case 'picture':
                    $order_type = 5;
                    break;
                case 'meeting':
                    $order_type = 8;
                    if ($info['is_draft']!=2) return $api->output(showErrorMsg('数据未发布',100002),I('post.'));
                    break;
                case 'question':
                    $order_type = 11;
                    if ($info['is_draft']!=2) return $api->output(showErrorMsg('数据未发布',100002),I('post.'));
                    break;
                default:
                    break;
            }


            if (!empty($is_login) && $token['id']==$info['uid']){//同一人
            }else{

                if ($info['auth']!=0 && empty($is_login)) return $api->output(showErrorMsg('登录后操作',100105),I('post.'),2,array('id'=>$info['id'],'uid'=>$info['uid']));

                if ($info['auth']==0){
                }elseif ($info['auth']==1){//粉丝可见

                    $is_follower = M('user_follow')->where(['uid'=>$token['id'],'puid'=>$info['uid']])->find();


                    $nickname = M('user')->where(['id'=>$info['uid']])->getField('nickname');

                    if (empty($is_follower)) return $api->output(showErrorMsg('成为粉丝后查看详情',100100,2,array('id'=>$info['id'],'uid'=>$info['uid'],'nickname'=>$nickname)),I('post.'));

                }elseif ($info['auth']==2){//会员或者付费

//                    $is_follower = M('user_follow')->where(['uid'=>$token['id'],'puid'=>$info['uid']])->find();

                    $is_member = M('order')->where(['uid'=>$token['id'],'work_id'=>$info['uid'],'type'=>12,'status'=>1,'deadline'=>['gt',time()]])->find();


                    $is_pay = M('order')->where(['uid'=>$token['id'],'work_id'=>$info['id'],'type'=>$order_type,'status'=>1])->find();

                    $nickname = M('user')->where(['id'=>$info['uid']])->getField('nickname');

                    if (empty($is_pay) && empty($is_member)) return $api->output(showErrorMsg('成为改该博主会员或者付费后可查看详情',100101,2,array('id'=>$info['id'],'uid'=>$info['uid'],'nickname'=>$nickname,'fee'=>$info['fee'])),I('post.'));

                }elseif ($info['auth']==3){//仅付费

                    $is_pay = M('order')->where(['uid'=>$token['id'],'work_id'=>$info['id'],'type'=>$order_type,'status'=>1])->find();
                    if (empty($is_pay) && empty($is_follower)) return $api->output(showErrorMsg('付费后可查看详情',100102,2,array('id'=>$info['id'],'uid'=>$info['uid'],'fee'=>$info['fee'])),I('post.'));

                }elseif ($info['auth']==4){//仅密码

                    if (empty($pwd)) return $api->output(showErrorMsg('输入密码后可查看详情',100103,2,array('id'=>$info['id'],'uid'=>$info['uid'])),I('post.'));

                    if ($pwd!=$info['pwd']) return $api->output(showErrorMsg('输入正确的密码后可查看详情',100104,2,array('id'=>$info['id'],'uid'=>$info['uid'])),I('post.'));

                }
            }
            return true;
        }
    }

    public function end($param)
    {
    }
}

/**
 * Class checkFamily
 * @package App\Controller
 */
class checkFamily implements Extension
{

    private $arr = array();

    public function addCheck(Extension $extension)
    {
        $this->arr[] = $extension;
    }

    public function before($param)
    {
        foreach ($this->arr as $extension) {
            $extension->before($param);
        }
    }

    public function end($param)
    {
        foreach ($this->arr as $extension) {
            $extension->end($param);
        }
    }
}